package com.jeremyfeinstein.utils;

import android.content.Context;
import android.support.annotation.StringRes;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by sunxunchao on 2017/8/15.
 */

public class ToastUtil {

    private static Toast mToast;

    public static void showToast( String msg) {
        if (mToast == null )
            mToast = Toast.makeText(ConferenceApp.getApp(), msg, Toast.LENGTH_LONG);
        else
            mToast.setText(msg);
        mToast.show();
    }

    public static void showLong(Context context, String msg) {
        if (mToast == null)
            mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        else
            mToast.setText(msg);
        mToast.show();
    }


    public static void showShort(Context context, String msg) {
        if (mToast == null)
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        else
            mToast.setText(msg);
        mToast.show();
    }

    public static void showLong(Context context, int msg) {
        if (mToast == null)
            mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        else
            mToast.setText(msg);
        mToast.show();
    }


    public static void showShort(Context context, int msg) {
        if (mToast == null)
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        else
            mToast.setText(msg);
        mToast.show();
    }

    public static void showGravity(Context context, @StringRes int msg, int gravity) {
        showGravity(context, msg, gravity, 0, 0);
    }

    public static void showGravity(Context context, @StringRes int msg, int gravity, int xOffset, int yOffset) {
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
            mToast.setGravity(gravity, xOffset, yOffset);
        } else {
            mToast.setText(msg);
        }

        mToast.show();
    }

    public static Toast showGravity(Context context, String msg, int gravity) {
        return showGravity(context, msg, gravity, 0, 0);
    }

    public static Toast showGravity(Context context, String msg, int gravity, int xOffset, int yOffset) {
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
            mToast.setGravity(gravity, xOffset, yOffset);
            TextView v = (TextView) mToast.getView().findViewById(android.R.id.message);
            v.setTextSize(21);
        } else {
            mToast.setText(msg);
        }
        return mToast;
//        mToast.show();
    }


    public static void cancelShow() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

    public static void showMyToast(final Toast toast, final int cnt) {
        final Timer timer =new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        },0,3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, cnt );
    }
}
